package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Users;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//        获取名字和密码
        String ck = req.getParameter("ck");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Users user = userService.login(username, password);
//        if ("1".equals(ck)) {
//            Users user1 = (Users) req.getSession().getAttribute("loginUser");
//            Cookie unCookie = new Cookie("username", user.getUsername());
//            Cookie pwdCookie = new Cookie("password", user.getPassword());
////           设置Cookie的有效期为10天
//            unCookie.setMaxAge(60 * 60 * 24 * 10);
//            pwdCookie.setMaxAge(60 * 60 * 24 * 10);
//            unCookie.setPath("/");
//            pwdCookie.setPath("/");
//            resp.addCookie(unCookie);
//            resp.addCookie(pwdCookie);
//        }
//        req.getRequestDispatcher("/pages/main.jsp").forward(req,resp);
        if (user != null) {
                req.getSession().setAttribute("loginUser", user);
                req.getSession().setAttribute("username", username);
                req.getRequestDispatcher("/pages/main.jsp").forward(req, resp);
        } else {
                req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
            }

    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
//        调用方法获取所有信息
        List<Users> list = userService.findAll(pageNum, pageSize);

//        使用分页插件
        PageInfo<Users> pageInfo = new PageInfo<>(list);

//        放入会话域
        req.setAttribute("page", pageInfo);

//        重定向到list,jsp页面
        req.getRequestDispatcher("/pages/user-list.jsp").forward(req, resp);
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.getSession().removeAttribute("loginUser");
        req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
    }

    private void changePassword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Users user = (Users) req.getSession().getAttribute("loginUser");
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
//
//        Users users = BeanUtil.fillBean(req, Users.class);

        userService.changePassword(user);

        req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
    }

    //跳转方法
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/pages/user-update.jsp").forward(req, resp);
    }

    private void rememberUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ck = req.getParameter("ck");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if ("1".equals(ck)) {
            Users user = (Users) req.getSession().getAttribute("loginUser");
            Cookie unCookie = new Cookie("username", user.getUsername());
            Cookie pwdCookie = new Cookie("password", user.getPassword());
//           设置Cookie的有效期为10天
            unCookie.setMaxAge(60 * 60 * 24 * 10);
            pwdCookie.setMaxAge(60 * 60 * 24 * 10);
            unCookie.setPath("/");
            pwdCookie.setPath("/");
            resp.addCookie(unCookie);
            resp.addCookie(pwdCookie);
        }
    }
}
